package com.bestcem.xm.survey.export.type;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.survey.export.BaseTypeHandler;
import com.bestcem.xm.survey.grpc.client.dto.QstructsDto;
import org.springframework.stereotype.Component;

import java.util.*;

import static com.bestcem.xm.survey.enums.QuestionTypeEnum.QUESTION_TYPE_PROPORTION;

/**
 * 比重题
 */
@Component
public class ProportionHandler implements BaseTypeHandler {

    @Override
    public Map<String, String> getSelectColumnChangeDictV2(JSONObject data, String dtype) {
        Map<String, String> column_change_dict = new HashMap();
        //选项选项
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        int size = items.size();
        items.forEach(v -> column_change_dict.put(getSelectRealcolV2(data, v), getSelectReadablecolV2(data, v, size)));
        return column_change_dict;
    }

    private String getSelectRealcolV2(JSONObject data, JSONObject item) {
        //cid:题目可见编号
        return String.format("%s__%s", data.getString("cid"), item.get("oid"));
    }

    private String getSelectReadablecolV2(JSONObject data, JSONObject item, int size) {
        //cid:题目可见编号
        if (size == 1) {
            return String.format("%s__分值", data.getString("title"));
        } else {
            return String.format("%s__%s", BaseTypeHandler.clearHtml(data.getString("title")), BaseTypeHandler.clearHtml(item.getString("title")));
        }
    }

    @Override
    public Map<String, String> getOpenColumnChangeDictV2(JSONObject data) {
        Map<String, String> res = new HashMap();
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        items.forEach(v -> {
            if (isOpenOption(v)) {
                res.put(getOpenRealcolV2(data, v), getOpenReadablecolV2(data, v));
            }
        });
        return res;
    }

    private String getOpenRealcolV2(JSONObject data, JSONObject item) {
        return String.format("%s__%s__open", data.getString("cid"), item.getString("id"));
    }

    private String getOpenReadablecolV2(JSONObject data, JSONObject item) {
        return String.format("%s__%s__开放", BaseTypeHandler.clearHtml(data.getString("title")), BaseTypeHandler.clearHtml(item.getString("title")));
    }

    @Override
    public List<String> getSelectColumnListV2(JSONObject data, String dtype) {
        List<String> column_list = new ArrayList();
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        items.forEach(v -> column_list.add(getSelectRealcolV2(data, v)));
        return column_list;
    }

    @Override
    public List<String> getOpenColumnListV2(JSONObject data) {
        List<String> res = new ArrayList();
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        items.forEach(s -> {
            if (isOpenOption(s)) {
                res.add(getOpenRealcolV2(data, s));
            }
        });
        return res;
    }

    @Override
    public Map<Object, Object> getSelectAnswerDictV2(JSONObject dataValue, Object answerValue, String dtype, Boolean score_apply_type) {
        Map<Object, Object> data = new HashMap<>();
        List<JSONObject> items = (List<JSONObject>) dataValue.getOrDefault("items", Collections.emptyList());
        if (Objects.isNull(answerValue)) {
            //np.nan 可以理解为null
            items.forEach(v -> data.put(getSelectRealcolV2(dataValue, v), null));
        } else {
            Map<String, Object> answers = (Map<String, Object>) answerValue;
            items.forEach(item -> {
                for (Map.Entry<String, Object> entry : answers.entrySet()) {
                    String gid = entry.getKey();
                    Object answer = entry.getValue();
                    if (gid.equals(item.getString("gid"))) {
                        if (answer instanceof Integer) {
                            data.put(getSelectRealcolV2(dataValue, item), answer);
                        } else if (answer instanceof List) {
                            //选项关联导致比重题的所有选项并没有全部选择
                            Integer value = (Integer) ((List) answer).get(0);
                            data.put(getSelectRealcolV2(dataValue, item), value);
                        } else {
                            //np.nan 可以理解为null
                            data.put(getSelectRealcolV2(dataValue, item), null);
                        }
                        break;
                    } else {
                        //np.nan 可以理解为null
                        data.put(getSelectRealcolV2(dataValue, item), null);
                    }
                }
            });
        }
        return data;
    }


    @Override
    public Map<Object, Object> getOpenAnswerDictV2(JSONObject dataValue, Object answerValue) {
        Map<Object, Object> data = new HashMap<>();
        List<JSONObject> items = (List<JSONObject>) dataValue.getOrDefault("items", Collections.emptyList());
        if (Objects.isNull(answerValue)) {
            items.forEach(item -> {
                if (isOpenOption(item)) {
                    data.put(getOpenRealcolV2(dataValue, item), null);
                }
            });
        } else {
            Map<String, List<Object>> answers = (Map<String, List<Object>>) answerValue;
            items.forEach(item -> {
                for (Map.Entry<String, List<Object>> entry : answers.entrySet()) {
                    String gid = entry.getKey();
                    List<Object> answer = entry.getValue();
                    if (gid.equals(item.getString("gid"))) {
                        if (isOpenOption(item)) {
                            if (answer.size() > 1) {
                                data.put(getSelectRealcolV2(dataValue, item), BaseTypeHandler.clearHtml(String.valueOf(answer.get(1))));
                            } else {
                                data.put(getSelectRealcolV2(dataValue, item), null);
                            }
                        }
                    }
                }
            });
        }
        return data;
    }

    @Override
    public List<List<String>> getVarLabelsV2(JSONObject dataValue, boolean not01, boolean need_title) {
        List<List<String>> data = new ArrayList<>();
        List<String> var_list = new ArrayList(), val_list = new ArrayList();
        List<JSONObject> items = (List<JSONObject>) dataValue.getOrDefault("items", Collections.emptyList());
        if (items.size() == 1) {
            JSONObject item = items.get(0);
            var_list.add(String.format("VARIABLE LABELS %s__%s '%s:分值'.", dataValue.get("cid"), item.get("oid"), getTitles(dataValue, null, item, false)));
        } else {
            items.forEach(item ->
                    var_list.add(String.format("VARIABLE LABELS %s__%s '%s'.", dataValue.get("cid"), item.get("oid"), getTitles(dataValue, null, item, false))));
        }
        data.add(var_list);
        data.add(val_list);
        return data;
    }

    @Override
    public List<String> getSpssCodeV2(JSONObject param, QstructsDto qstruct) {
        List<JSONObject> items = (List<JSONObject>) param.getOrDefault("items", Collections.emptyList());
        List<String> head_list = new ArrayList<>();
        items.stream().forEach(item -> {
            head_list.add(String.format("%s__%s", param.get("cid"), item.get("oid")));
            if (item.getBoolean("is_open") && item.getBoolean("is_open")) {
                head_list.add(String.format("%s__%s__open", param.get("cid"), item.get("oid")));
            }
        });
        return head_list;
    }

    @Override
    public String getQuestionTypeValue() {
        return QUESTION_TYPE_PROPORTION.getValue();
    }
}
